//
//  LXNavBarView.m
//  cat
//
//  Created by imera on 16/3/9.
//  Copyright © 2016年 LX. All rights reserved.
//

#import "LXNavBarView.h"
#import "LXCostumButton.h"

@interface LXNavBarView()
/** 左边按钮 */
@property (nonatomic, weak) LXCostumButton *backBtn;

@end

@implementation LXNavBarView

- (void)setLeftTitle:(NSString *)leftTitle
{
    if (![leftTitle isEqualToString:@""]) {
        _leftTitle = leftTitle;
    }
    [self layoutSubviews];
}

- (void)setRightTitle:(NSString *)rightTitle
{
    if (![rightTitle isEqualToString:@""]) {
        _rightTitle = rightTitle;
    }
    [self layoutSubviews];
    self.rightBtn.hidden = NO;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self){
        self.backgroundColor = LXColor(41, 188, 208);
        
        LXCostumButton *backBtn = [LXCostumButton buttonWithType:UIButtonTypeCustom];
        backBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        [backBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [backBtn setImage:[UIImage imageNamed:@"arrow"] forState:UIControlStateNormal];
        [backBtn setTitle:@"返回" forState:UIControlStateNormal];
        [backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
        self.backBtn = backBtn;
        [self addSubview:backBtn];
        
        UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        rightBtn.hidden = YES;
        rightBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        [rightBtn setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled];
        [rightBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [rightBtn addTarget:self action:@selector(rightBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:rightBtn];
        self.rightBtn = rightBtn;
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    /** 左侧按钮 */
    [self.backBtn setTitle:self.leftTitle forState:UIControlStateNormal];
    CGSize backBtnSize = [self.backBtn.titleLabel.text sizeWithAttributes:@{NSFontAttributeName:self.backBtn.titleLabel.font}];
    CGFloat backBtnW = backBtnSize.width + 24;
    CGFloat backBtnH = 30;
    CGFloat backBtnX = LXMovieShowViewControllerMargin;
    CGFloat backBtnY = (self.frame.size.height - backBtnH) / 2;
    self.backBtn.frame = CGRectMake(backBtnX, backBtnY, backBtnW, backBtnH);

    /** 右侧按钮 */
    [self.rightBtn setTitle:self.rightTitle forState:UIControlStateNormal];
    CGSize rightBtnSize = [self.rightBtn.titleLabel.text sizeWithAttributes:@{NSFontAttributeName:self.rightBtn.titleLabel.font}];
    CGFloat rightBtnW = rightBtnSize.width;
    CGFloat rightBtnH = 30;
    CGFloat rightBtnX = self.frame.size.width - rightBtnW - 10;
    CGFloat rightBtnY = (self.frame.size.height - backBtnH) / 2;
    self.rightBtn.frame = CGRectMake(rightBtnX, rightBtnY, rightBtnW, rightBtnH);

}

- (void)back
{
    if ([self.myDelegate respondsToSelector:@selector(popCurrentViewController:)]) {
        [self.myDelegate popCurrentViewController:self];
    }
}

- (void)rightBtnClick
{
    if ([self.myDelegate respondsToSelector:@selector(rightBtnClicked:)]) {
        [self.myDelegate rightBtnClicked:self];
    }
}
@end
